package com.whl.product.service.impl;

import com.whl.basic.exception.BusinessException;
import com.whl.basic.service.IBaiduAiAuditService;
import com.whl.basic.util.LoginContext;
import com.whl.org.domain.Employee;
import com.whl.org.mapper.EmployeeMapper;
import com.whl.product.domain.Product;
import com.whl.product.domain.ProductDetail;
import com.whl.product.domain.ProductOnlineAuditLog;
import com.whl.product.mapper.ProductDetailMapper;
import com.whl.product.mapper.ProductMapper;
import com.whl.product.mapper.ProductOnlineAuditLogMapper;
import com.whl.product.service.IProductService;
import com.whl.basic.service.impl.BaseServiceImpl;
import com.whl.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    /**
     * 重构添加
     * @param product
     */
    @Override
    public void add(Product product) {
        super.add(product);

        // 获取服务详情对象
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail != null) { // 说明输入了明细
            // 重置服务明细的服务id
            productDetail.setProductId(product.getId());
            // 添加到数据库
            productDetailMapper.save(productDetail);
        }
    }

    /**
     * 重构修改
     * @param product
     */
    @Override
    public void update(Product product) {
        super.update(product);
        // 获取服务详情对象
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail != null) { // 说明输入了明细
            // 重置服务明细的服务id
            productDetail.setProductId(product.getId());
            // 添加到数据库
            productDetailMapper.updateProductDetailByProductDetailId(productDetail);
        }
    }

    /**
     * 重构删除
     * @param id
     */
    @Override
    public void delete(Long id) {
        super.delete(id);
        // 根据服务id删除服务明细id
        productDetailMapper.deleteProductDetailByProductId(id);
    }

    /**
     * 上架
     * @param ids
     * @param request
     */
    @Override
    public void onsale(List<Long> ids, HttpServletRequest request) {
        // 获取登录人信息
        Logininfo logininfo = LoginContext.getLoginUser(request);
        // 获取审核人
        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());
        for (Long id : ids) {
            // 获取服务对象
            Product product = productMapper.findOne(id);
            Boolean textAuditBoo = baiduAiAuditService.textAudit(product.getName());
            Boolean imageAuditBoo = baiduAiAuditService.imageAudit(product.getResources());
            if(textAuditBoo && imageAuditBoo){ // 为true
                // 审核通过：修改服务状态 + 上架时间
                product.setState(1L);
                product.setOnsaletime(new Date());
                productMapper.update(product);

                // 添加服务审核日志
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setState(1);
                log.setAuditTime(new Date());
                log.setAuditId(employee.getId());
                log.setNote("审核通过!!!");
                log.setProductId(product.getId());
                productOnlineAuditLogMapper.save(log);
            }else { // 审核失败
                // 添加上架审核失败的日志
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setState(0);
                log.setAuditTime(new Date());
                log.setAuditId(employee.getId());
                log.setNote("审核失败，有非法内容!!!");
                log.setProductId(product.getId());
                productOnlineAuditLogMapper.save(log);
                throw new BusinessException("审核失败，有非法内容!!!");
            }
        }

    }

    /**
     * 下架
     * @param ids
     */
    @Override
    public void offsale(List<Long> ids) {
        Date offTime = new Date();
        productMapper.offsale(ids,offTime);
    }
}
